package cn.elead.chaos.core.map;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cn.elead.chaos.core.list.Lists;


/**
 * 
 * @Auth 赵光
 * @Describle 等价于Map<String, Collection<V>>, 功能增强
 */
public class TableMap<V> extends DefaultMap<Collection<V>> {

	private static final long serialVersionUID = 1L;

	public TableMap() {

	}

	public TableMap(String key, Collection<V> Collection) {
		put(key, Collection);
	}

	public TableMap(Map<String, Collection<V>> map) {
		putAll(map);
	}

	@SuppressWarnings("unchecked")
	public TableMap<V> addValue(String key, V... values) {
		Collection<V> collection = get(key);
		if (collection == null) {
			collection = Lists.newList();
		}
		collection.addAll(Arrays.asList(values));
		put(key, collection);
		return this;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<V> getValue(String key) {
		return Lists.newList(get(key));
	}

	public void removeBatch(Set<String> keys) {
		for (String key : keys) {
			remove(key);
		}
	}

	public boolean containsValue(String key, V value) {
		return this.containsKey(key) && this.get(key).contains(value);
	}

	public static <V> TableMap<V> newMap() {
		return new TableMap<>();
	}

	@SuppressWarnings("unchecked")
	public static <V> TableMap<V> newMap(String key, V... value) {
		return new TableMap<V>().addValue(key, value);
	}

	public static <V> TableMap<V> newMap(Map<String, Collection<V>> map) {
		return new TableMap<>(map);
	}

}